package com.zycfc.zsf.boot.core.builder;

import org.springframework.core.*;
import org.springframework.boot.*;
import org.springframework.core.env.*;
import com.zycfc.zsf.boot.util.text.*;
import com.zycfc.zsf.boot.core.enums.*;
import org.springframework.context.*;
import java.util.*;
import java.util.regex.*;
import org.slf4j.*;

public class ZsfSpringApplicationRunListener implements SpringApplicationRunListener, PriorityOrdered
{
    private static final Logger log;
    SpringApplication application;
    
    public ZsfSpringApplicationRunListener(final SpringApplication application, final String... args) {
        this.application = application;
    }
    
    public void starting() {
    }
    
    public void environmentPrepared(final ConfigurableEnvironment environment) {
        String profile = this.getProfileFromYml();
        final String activeProfile = System.getProperty("spring.profiles.active");
        ZsfSpringApplicationRunListener.log.info("\u542f\u52a8\u53c2\u6570\u4e2d[spring.profiles.active]\u503c\u4e3a:{}", (Object)activeProfile);
        if (StringUtils.isNotEmpty(activeProfile)) {
            profile = activeProfile;
        }
        if (StringUtils.isEmpty(profile)) {
            ZsfSpringApplicationRunListener.log.error("activeProfile \u5bf9\u5e94\u7684\u73af\u5883\u53c2\u6570\u4e0d\u5b58\u5728 ");
            return;
        }
        ZsfSpringApplicationRunListener.log.debug("\u5f53\u524d\u73af\u5883:{}", (Object)profile);
        final String clusterName = this.getClusterName();
        this.assembleActiveProfile(environment, profile, clusterName);
    }
    
    private String getProfileFromYml() {
        final Object key = YamlReader.instance.getValueByKey("application.yml", "spring.profiles.active");
        final List<String> list = this.getFields(String.valueOf(key));
        if (StringUtils.isEmpty((List)list)) {
            ZsfSpringApplicationRunListener.log.error("application.yml\u4e2d[spring.profiles.active]\u53c2\u6570\u4e3a\u7a7a");
            return null;
        }
        final String profile;
        final String profileStr = profile = list.get(0).split(":")[1];
        ZsfSpringApplicationRunListener.log.info("application.yml\u4e2d[spring.profiles.active]\u503c\u4e3a:{}", (Object)profile);
        return profile;
    }
    
    private String getClusterName() {
        String clusterName = "core";
        final Object obj = YamlReader.instance.getValueByKey("application.yml", "zsf.all.cluster.name");
        if (obj == null) {
            ZsfSpringApplicationRunListener.log.debug("application.yml\u4e2d[zsf.all.cluster.name]\u503c\u4e3a\u7a7a,\u9ed8\u8ba4\u52a0\u8f7dcore\u96c6\u7fa4");
        }
        else {
            clusterName = String.valueOf(obj);
            ZsfSpringApplicationRunListener.log.debug("application.yml\u4e2d[zsf.all.cluster.name]\u503c\u4e3a:{}", (Object)clusterName);
        }
        final String sysClusterName = System.getProperty("zsf.all.cluster.name");
        ZsfSpringApplicationRunListener.log.debug("\u542f\u52a8\u53c2\u6570\u4e2d[zsf.all.cluster.name]\u503c\u4e3a:{}", (Object)sysClusterName);
        if (StringUtils.isNotEmpty(sysClusterName)) {
            clusterName = sysClusterName;
        }
        return clusterName;
    }
    
    private void assembleActiveProfile(final ConfigurableEnvironment environment, final String profile, final String clusterName) {
        final Object system = YamlReader.instance.getValueByKey("application.yml", "zsf.boot.ds.dbNamePrefix");
        final String linkSymbol = "-";
        final String globalKey = "global";
        final String dsKey = "ds";
        final String projectKey = "project";
        if (ClusterEnum.core.name().equals(clusterName)) {
            environment.addActiveProfile(globalKey);
            environment.addActiveProfile(globalKey + linkSymbol + profile);
            environment.addActiveProfile(dsKey + linkSymbol + profile);
            if (system != null) {
                environment.addActiveProfile(dsKey + linkSymbol + profile + linkSymbol + String.valueOf(system));
            }
            environment.addActiveProfile(projectKey + linkSymbol + profile);
            return;
        }
        environment.addActiveProfile(clusterName + linkSymbol + profile);
        environment.addActiveProfile(clusterName + linkSymbol + globalKey);
        environment.addActiveProfile(clusterName + linkSymbol + globalKey + linkSymbol + profile);
        environment.addActiveProfile(clusterName + linkSymbol + dsKey + linkSymbol + profile);
        if (system != null) {
            environment.addActiveProfile(clusterName + linkSymbol + dsKey + linkSymbol + profile + linkSymbol + String.valueOf(system));
        }
        environment.addActiveProfile(clusterName + linkSymbol + projectKey + linkSymbol + profile);
    }
    
    public void contextPrepared(final ConfigurableApplicationContext context) {
    }
    
    public void contextLoaded(final ConfigurableApplicationContext context) {
    }
    
    public void started(final ConfigurableApplicationContext context) {
    }
    
    public void running(final ConfigurableApplicationContext context) {
    }
    
    public void failed(final ConfigurableApplicationContext context, final Throwable exception) {
    }
    
    public int getOrder() {
        return 100;
    }
    
    private List<String> getFields(final String sql) {
        return this.getFields("$", sql);
    }
    
    private List<String> getFields(final String prefix, final String sql) {
        final List<String> list = new ArrayList<String>();
        final Pattern regex = Pattern.compile("\\" + prefix + "\\{([^}]*)\\}");
        final Matcher matcher = regex.matcher(sql);
        while (matcher.find()) {
            list.add(matcher.group(1));
        }
        return list;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ZsfSpringApplicationRunListener.class);
    }
}
